// pages/outvisit/task/inputInfo.js
var api = require("../../../utils/api.js");
var common = require("../../../utils/common.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pickerValue: [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],//下拉框的值
    id: 0,//任务ID
    pid:'709',
    detail: {},//查勘详情
    appointdate: "",//外访日期
    isNoUpdate: true,//是否不允许更新
    testList: common.testList,
    surveyColumnValList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var id = options.id;

    // 设置任务ID
    that.setData({
      id: id
    }, () => {
      // 获取外访记录
      that.getSuveyDetails();
      that.getSuveyColumnVal();
    });
  },

  // 获取任务详情
  getSuveyDetails:function(){
    var that=this;
    wx.showLoading({
      title: "加载中",
      mask: true
    });//显示加载中
    api.getAppointmentDetails(that.data.id).then(res => {
      if (res.code == 0) {
        wx.hideLoading();
        var data = res.data;
        // 更新数据
        that.setData({
          detail: data,
        }, () => {
          // 合并录音
        });
      }
    });
  },

  // 获取任务详情
  getSuveyColumnVal:function(){
    var that=this;
    wx.showLoading({
      title: "加载中",
      mask: true
    });//显示加载中
    api.getSuveyColumnVal(that.data.id, that.data.pid).then(res => {
      if (res.code == 0) {
        //wx.hideLoading();
        var data = res.data;
        console.log("that.data================>");

        // 更新数据
        that.setData({
          surveyColumnValList: data.colValueList,
          pickerValue: data.pickerValue
        }, () => {
          //...
        });
      }
    });
  },

  // 设置表单下拉框默认值
  setPickerValue: function (data) {
    var that = this;
    var pickerValue = that.data.pickerValue;

    console.log(data.returnTypeList);
    if (data.returnTypeList) {
      pickerValue[0] = data.returnTypeList;
    }
    that.setData({
      pickerValue: pickerValue
    });
  },

  // 提交表单
  formSubmit: function (e) {
    var that = this;
    if (that.data.isNoUpdate){
      wx.showModal({
        title: "提示",
        content: "确定要提交吗？",
        success: function (sm) {
          if (sm.confirm) {
            // 提交表单数据
            that.formSubmitData(e);
          }
        }
      })
    } else {
      // 提交表单数据
      that.formSubmitData(e);
    }
  },

  // 提交表单数据
  formSubmitData: function (e) {
    var that = this;
    var data = e.detail.value;
    var submitData = {};
    for (var d in data) {
      submitData[d] = data[d];
    }
    //console.log(submitData);
    var surveyColumnValList = that.data.surveyColumnValList;
    var buildYear;
    var buildStructure;

    for (var i = 0; i < surveyColumnValList.length; i++) {
      var key = surveyColumnValList[i].id;
      var surveyText = surveyColumnValList[i].text;

      if (surveyColumnValList[i].isrequired == 1){
        if (submitData[key] == null || submitData[key] == '' || submitData[key].length == 0) {
          wx.showToast({
            title: '['+surveyText+']必填，请填写！',
            icon: 'none'
          });

          return;
        }
      }

      if (key == '627') {
        buildYear = submitData[key];
      }
      if (key == '618') {
        buildStructure = submitData[key];
        console.log(`buildStructure:${buildStructure}`);
      }
    }

    var newCalNewessRate = "0";

    for (var i = 0; i < surveyColumnValList.length; i++) {
      var key = surveyColumnValList[i].id;
      var surveyText = surveyColumnValList[i].text;

      if (key == '679') {
        const now = new Date();
        const year = now.getFullYear();
        var diffYear = year - buildYear;
        var key = surveyColumnValList[i].id;
        var selectValue = surveyColumnValList[i].inputValue;

        var totalYear = 60;
        if (selectValue == '55' || selectValue == '54' || selectValue == '57' || selectValue == '1306') {
          totalYear = 60;
        }
        else if (selectValue == '56' || selectValue == '1307' ) {
          totalYear = 50;
        }
        else if (selectValue == '58') {
          totalYear = 40;
        }
        else if (selectValue == '936') {
          totalYear = 16;
        }

        console.log(`差异年份:${diffYear}，总年份：${totalYear}`);
        var calNewessRate = 1-(diffYear/totalYear);
        calNewessRate = calNewessRate.toFixed(2);

        console.log(`成新率:${calNewessRate}`);
        submitData[key] = calNewessRate;
      }
    }

    wx.showLoading({
      title: '提交中'
    });//显示加载中
    api.addSurveyColumnValueForApp(submitData,surveyColumnValList,"建筑物实物状况").then(res => {
      wx.hideLoading();
      if (res.code == 0) {
        wx.showToast({
          title: "提交成功",
          icon: "none"
        });

        let pages = getCurrentPages();//当前页面
        let prevPage = pages[pages.length - 2];//上一页面
        prevPage.getSurveyColumnComplete();

        // 返回
        wx.navigateBack({
          delta: 1,
        });
      }
    });
  },


  //时间选择
  bindDateChange(e) {
    let that = this;
    that.setData({
      appointdate: e.detail.value
    })
  },

  bindTimeChange: function(e) {
    this.setData({
      appointtime: e.detail.value
    })
  },

  radioChange: function(e) {
    var that = this;
    var selectValue = e.detail.value;
    console.log(selectValue);

    var detail = that.data.detail;
    var surveyColumnValList = that.data.surveyColumnValList;

    var submitData = {};
    for (var d in detail) {
      submitData[d] = detail[d];
    }

    if (selectValue == '202' || selectValue == '203' || selectValue == '2012') {
      for (var i = 0; i < surveyColumnValList.length; i++) {
        var key = surveyColumnValList[i].id;
        var surveyText = surveyColumnValList[i].text;
        //console.log(key);

        if (key == '1010') {
          //console.log(key);
          surveyColumnValList[i].isrequired = false;
          surveyColumnValList[i].isshow = true;

          this.setData({
            [`surveyColumnValList[${i}].isrequired`]:false,
            [`surveyColumnValList[${i}].isshow`]:1,
          });
        }
      }
    }
    //与产权地址一致性 - 不一致
    else if (selectValue == '2013') {
      for (var i = 0; i < surveyColumnValList.length; i++) {
        var key = surveyColumnValList[i].id;
        var surveyText = surveyColumnValList[i].text;

        if (key == '1010') {
          //console.log(key);
          surveyColumnValList[i].isrequired = true;
          surveyColumnValList[i].isshow = false;

          this.setData({
            [`surveyColumnValList[${i}].isrequired`]:true,
            [`surveyColumnValList[${i}].isshow`]:0,
          });
        }
      }
    }

    //console.log(detail);
    //console.log(surveyColumnValList);
    if (selectValue == '1941' || selectValue == '1933' || selectValue == '1932' || selectValue == '1931'
        || selectValue == '1939' || selectValue == '1938' || selectValue == '1937' || selectValue == '1935'
        || selectValue == '1934' || selectValue == '1940' || selectValue == '1936') {
      var surveyColumnValList = that.data.surveyColumnValList;
      //surveyColumnValList[i].inputValue = selectValue;

      var buildYear;
      var buildStructure = selectValue;

      for (var i = 0; i < surveyColumnValList.length; i++) {
        var key = surveyColumnValList[i].id;
        var surveyText = surveyColumnValList[i].text;

        if (key == '627') {
          buildYear = surveyColumnValList[i].inputValue;
          //console.log(`buildYear:${buildYear}`);
        }
        if (key == '1019') {
          surveyColumnValList[i].inputValue = selectValue;
          //console.log(`buildStructure2:${surveyColumnValList[i].inputValue}`);
        }
      }

      for (var i = 0; i < surveyColumnValList.length; i++) {
        var key = surveyColumnValList[i].id;
        var surveyText = surveyColumnValList[i].text;

        if (key == '679') {
          const now = new Date();
          const year = now.getFullYear();
          var diffYear = year - buildYear;
          var key = surveyColumnValList[i].id;

          var totalYear = 60;

          if (selectValue == '1933' || selectValue == '1932' || selectValue == '1931' || selectValue == '1938'
              || selectValue == '1934' || selectValue == '1940'  || selectValue == '1936') {
            totalYear = 60;
          }
          else if (selectValue == '1937' || selectValue == '1935'  || selectValue == '1935' ) {
            totalYear = 50;
          }
          else if (selectValue == '1941' || selectValue == '1935' || selectValue == '1941') {
            totalYear = 40;
          }
          else {
            totalYear = 50;
          }

          console.log(`差异年份:${diffYear}，总年份：${totalYear}`);
          var calNewessRate = 1-(diffYear/totalYear);
          calNewessRate = (calNewessRate.toFixed(2))*100;
          console.log(`成新率:${calNewessRate}`);
          surveyColumnValList[i].inputValue = calNewessRate;

          this.setData({
            [`surveyColumnValList[${i}].inputValue`]:calNewessRate,
          });
        }

        if (key == '1019') {
          var colValueList = surveyColumnValList[i].colValueList;
          for (var j=0; j<colValueList.length; j++) {
            var valueKey = colValueList[j].id;
            console.log("valueKey=============>"+valueKey+",selectValue=========>"+selectValue);
            if (valueKey == selectValue) {
              colValueList[j].checked = true;
            }
            else {
              colValueList[j].checked = false;
            }
          }

          this.setData({
            [`surveyColumnValList[${i}].colValueList`]:colValueList,
          });
        }
      }
    }

    this.setData({
      selectedGender: e.detail.value
    });
  },

  //下拉框选择事件
  bindPickerChange: function (e) {
    let pickerValue = this.data.pickerValue;
    pickerValue[parseInt(e.currentTarget.dataset.id)] = e.detail.value;

    console.log(this.data.pickerValue);
    this.setData({
      pickerValue: pickerValue
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
